package com.ellwood.modules.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ellwood.common.core.base.ServiceFactoryImpl;
import com.ellwood.common.core.constant.Constant;
import com.ellwood.common.core.result.PageResult;
import com.ellwood.dataMapper.system.entity.UserEntity;
import com.ellwood.dataMapper.system.mapper.UserMapper;
import com.ellwood.modules.system.convert.UserConvert;
import com.ellwood.modules.system.domain.VO.user.UserEditVO;
import com.ellwood.modules.system.domain.VO.user.UserPageQueryVO;
import com.ellwood.modules.system.domain.VO.user.UserQueryVo;
import com.ellwood.modules.system.domain.VO.user.UserSaveVo;
import com.ellwood.modules.system.domain.dto.UserPageQueryDto;
import com.ellwood.modules.system.domain.dto.UserQueryDto;
import com.ellwood.modules.system.logic.user.UserLogic;
import com.ellwood.modules.system.service.UserService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ProjectName: Ellwood-framework-boot
 * @PackageName: com.ellwood.modules.system.service.impl
 * @ClassName: UserServiceImpl
 * @Description: // 用户service接口实现
 * @Author: Ellwood
 * @CreateDate: 2024/7/22 - 下午1:33
 * @Version: V1.0.0
 */
@AllArgsConstructor
@Service
public class UserServiceImpl extends ServiceFactoryImpl<UserMapper, UserEntity> implements UserService {


    @Override
    public List<UserQueryDto> findAll(UserQueryVo queryVo) {
        List<UserEntity> userEntityList = baseMapper.selectList(UserLogic.getWrapper(queryVo));
        return UserConvert.INSTANCE.convertQueryDtoList(userEntityList);
    }

    @Override
    public UserQueryDto findById(Long id) {
        UserEntity userEntity = baseMapper.selectById(id);
        return UserConvert.INSTANCE.convertQueryDto(userEntity);
    }

    @Override
    public PageResult<UserPageQueryDto> pageQuery(UserPageQueryVO pageVo) {
        // 查询参数
        Map<String, Object> params = UserLogic.getParams(pageVo);
        // 分页查询
        IPage<UserEntity> page = this.getPage(pageVo);
        params.put(Constant.PAGE, page);
        // 数据列表
        List<UserEntity> userList = baseMapper.getUserList(params);
        return new PageResult<>(UserConvert.INSTANCE.convertPageQueryDtoList(userList), page.getTotal());
    }

    @Override
    public void saveUser(UserSaveVo userVo) {

        UserEntity userEntity = new UserLogic(baseMapper).getAddUserEntity(userVo);

        // 保存用户
        baseMapper.insert(userEntity);
    }


    @Override
    public void updateUser(UserEditVO userVo) {
        UserEntity userEntity = new UserLogic(baseMapper).getEditUserEntity(userVo);

        baseMapper.updateById(userEntity);

    }


    @Override
    public boolean delete(List<Long> idList) {
        // 删除用户角色关系 TODO
        // 删除用户岗位关系 TODO

        // 删除用户

        baseMapper.deleteByIds(idList);
        return false;
    }
}
